segments R Function 您所在的位置:网站首页 segment r语言 segments R Function

segments R Function

#segments R Function | 来源: 网络整理| 查看: 265

The segments R Function | 3 Example Codes

 

Basic R Syntax:

segments(x0 = 1, y0 = 1, x1 = 2, y1 = 2)

 

The segments R function draws a line segment between two pairs of points. The basic syntax for segments in R is shown above.

In the following article, I’m going to show you three examples for the application of the segments command in the R programming language.

So if you want to know more about the segments function, keep reading…

 

Example 1: Basic Application of the segments R Function

The basic usage of the segments R function is quite straight forward. Let’s first create an empty plot, to which we can add a line segment:

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot

 

Segments R Function Example - Empty R Plot

Graphic 1: Empty Plot as Preparation for Segments R Example.

 

Now, let’s apply the segments R function in order to draw a line segment:

segments(x0 = - 1, y0 = - 0.5, x1 = 0.5, y1 = 0) # Draw one line

 

Segments R Function Example - One Line Segment

Graphic 2: Basic Application of segments() Function.

 

As you can see, we had to specify four different values within the segments function:

x0 & y0: The x-y coordinates of the starting point from which to draw the line segment. x1 & y1: The x-y coordinates of the ending point to which to draw the line segment.

That’s it!

By the way, you may also have a look at the following YouTube video. In the video, I explain the R programming code of Example 1.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

However, the segments R function is very flexible. In the next example I’m going to show you some ways to make the line segments of your graphic much prettier…

 

Example 2: How to Modify Color, Thickness & Line Type

In the following example, I’m going to show you how to modify color, thickness and line type of your line segments. First, we need to create an empty plot (as in Example 1):

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot

We then can apply the segments command as in Example 1, but with additional specifications:

segments(x0 = - 1, y0 = - 0.5, x1 = 0.5, y1 = 0, # Draw one line as in Example 1 col = "cornflowerblue", # Color of line lwd = 5, # Thickness of line lty = "dotted") # Line type

 

Segments R Function Example - Line Segment with Color, Different Thikness and Dotted Line Type

Graphic 3: Manual modifications of segments() Function: Color, Thickness & Line Type.

 

As you can see, I changed the color, the thickness, and the line type of our line segment. However, many other arguments for modifying line segments can be found in the par help documentation.

 

Example 3: Add Multiple Line Segments to R Plot

The segments R function can also be useful, when you need to draw multiple lines to your graph. Again, let’s start with an empty figure:

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot

Now, let’s crate a data frame, in which we specify multiple starting and ending x-y coordinates:

mult_seg



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有